Skip to main content
The Cloudflare Worker provides AI agent capabilities and sandbox execution for Duet. It acts as a load balancer that distributes requests to Durable Objects (one per room).

Architecture

The Worker uses several Cloudflare services:
  • Cloudflare Workers - Request routing and API endpoints
  • Durable Objects - Per-room LLM context and memory
  • Cloudflare AI (Llama) - AI inference for the agent
  • Cloudflare Sandboxes - Isolated command execution environment
Sandbox features require a paid Cloudflare plan. AI inference and Durable Objects work on the free tier.

Prerequisites

  • Cloudflare account
  • Wrangler CLI installed
  • Bun or Node.js for local development

Setup

1

Navigate to worker directory

2

Install dependencies

3

Configure wrangler.toml

The wrangler.toml file is already configured with:
Update the name field to your desired Worker name.
4

Deploy to Cloudflare

Deploy the Worker:
Or with npm:
After deployment, you’ll receive a Worker URL like:

Local Development

To run the Worker locally for development:
When running locally, AI inference still happens on Cloudflare’s edge, not locally.

Worker Configuration

Durable Objects

The Worker uses two Durable Objects:
  1. DuetAgent - Manages per-room chat history and AI context
    • Stores the last 20 messages for context
    • Handles AI inference requests
    • Maintains conversation state
  2. Sandbox - Provides isolated command execution
    • One sandbox instance per room
    • Allows AI agent to run commands
    • Supports direct command execution by users

Migrations

Durable Object migrations are configured in wrangler.toml:

Container Configuration

The Sandbox Durable Object uses a custom container:

API Endpoints

The Worker exposes the following endpoints:

Health Check

Returns ok if the Worker is running.

Room Messages

Send a message to the AI agent for a specific room.

Sandbox Execution

Execute a command in the room’s sandbox.

Room Cleanup

Clean up a room and its associated Durable Objects.

Usage with Duet Server

After deploying, use your Worker URL when starting the Duet Go server:
Or in your Docker deployment:

Monitoring

Observability is configured in wrangler.toml:
Enable logs in production by setting enabled = true.

Troubleshooting

If sandbox features aren’t working, verify you have a paid Cloudflare plan that supports Workers with Containers.

Common Issues

  • 404 errors: Ensure room ID is included in the request path
  • Sandbox not available: Check if you have a paid Cloudflare plan
  • AI inference failing: Verify AI binding is configured in wrangler.toml
  • Durable Objects not persisting: Check migrations are applied correctly